<?php
/**
 * @author Eli Holder
 * @link www.eliholder.com
 * @copyright  Copyright (c) 2009 Eli Holder
 * @license    New BSD License
*/

class ZAMFExplorer {
	
	/**
	 * exploreServices - Go through each service in this directory and convert the php classes 
	 *
	 * Note: Requires the docblock (at)param for each param on a method
	 * it must be in the format '(at)param paramType paramName' comment
	 * 
	 * 
	 * @return array of ZAMFExplorerClass
	 */
	public function exploreServices() {
		
		//Get the list of services
		$services=  $this->_listServices();
		$classList= array();
		//For each service name
		foreach ( $services as $service ) {
			//Include the file
			include_once "$service.php";
			try {
				//Create a new ReflectionClass from class $service
				$refClass= new ReflectionClass( $service );
				//Get the public methods from the class
				$methods= $refClass->getMethods( ReflectionMethod::IS_PUBLIC );
				//Create a new ZAMFExplorerClass to hold the data from the current class
				$tempClass= new ZAMFExplorerClass();
				//Store the class' name
				$tempClass->name= $refClass->getName();
				
				//Foreach method in refClass
				foreach ( $methods as $refMeth ) {
					//Create a new ZAMFExplorerMethod to hold the method data
					$tempMethod= new ZAMFExplorerMethod();
					//Store the method's name
					$tempMethod->name= $refMeth->getName();
					if ( "_" === $tempMethod->name[0] ) {
						continue;
					}
					//Get the doc block string from the method
					$docBlock= $refMeth->getDocComment();

					//If the docBlock exists
					if ( is_string( $docBlock ) && strlen( $docBlock ) > 0 ) {
						//Get all the param strings from the docblock
						$matches= array();
						preg_match_all( "/@param(.*)\n/", $docBlock, $matches );

						//For each line of params
						foreach ( $matches[1] as $paramMatch ) {
							
							//Create an explorer param to hold the parameter info
							$tempParam= new ZAMFExplorerParam();
							//Split the line into "type", "name", "comment"
							$splitParam= preg_split( "/[ ]+/", trim( $paramMatch ), 3 );
							//Store the param type
							if( isset ( $splitParam[0] ) ) {
								$tempParam->type = $this->convertToAS3( trim ( $splitParam [0] ) );
							}
							//Store the Param name
							if ( isset( $splitParam [1] ) ) {
								$tempParam->name= trim( $splitParam [1] );
							}
							
							//Add the param to the method list
							$tempMethod->params[]= $tempParam;
						}
					} else { //If no docblock is listed, get the params from the refmethod
						/*foreach ( $refMeth->getParameters () as $param ) {
							//$param= new ReflectionParameter();
							$tempParam = new ZAMFExplorerParam ( );
							$tempParam->name = $param->getName ();
							$tempParam->type = "*";
							$tempMethod->params [] = $tempParam;
						}*/
					}
					//Add the method to the class list
					$tempClass->methods[]= $tempMethod;
				}
				
				//
				$classList[]= $tempClass;
			} catch ( Exception $ex ) {
				//skip
			}
		}

		//Return the array of classes
		return $classList;
	
	}
	
	/**
	 * convertToAS3 - translate php type strings to AS3 type strings
	 *
	 * @param string $inTypeString
	 * @return string
	 */
	private function convertToAS3( $inTypeString ) {
		if ( strcasecmp( $inTypeString, 'int' ) === 0 ) {
			return 'int';
		}
		if ( strcasecmp( $inTypeString, 'String' ) === 0 ) {
			return 'String';
		}
		return 'Object';
	}
	
	/**
	 * _listServices - Code taken and modified from AmfPHP
	 *
	 * @return Array of strings
	 */
	private function _listServices() {
		$dir = dirname ( realpath ( __FILE__ ) ) . '/';
		
		$services = array ( );
		if ($handle = opendir ( $dir)) {
			while ( false !== ($file = readdir ( $handle )) ) {
				chdir ( dirname ( __FILE__ ) );
				if ($file != "." && $file != "..") {
					if (is_file ( $dir . $file )) {
						if (strpos ( $file, '.methodTable' ) !== FALSE) {
							continue;
						}
						$index = strrpos ( $file, '.' );
						$before = substr ( $file, 0, $index );
						$after = substr ( $file, $index + 1 );
						
						if ($after == 'php') {
							//Ignore this class
							if ( strcasecmp( $before, __CLASS__ ) !== 0 ) {
								$services[] = $before;
							}
						}
					}
				}
			}
		} else {
			//echo("error");
		}
		closedir ( $handle );
		return $services;
	}

}

class ZAMFExplorerClass {
	public $name;
	public $methods = array ( );
}

class ZAMFExplorerMethod {
	public $name;
	public $params = array ( );
}

class ZAMFExplorerParam {
	public $type;
	public $name;
}

